package com.chinacoal.hr.levelimpl.service.impl;

import com.chinacoal.hr.levelapi.entity.Organization;
import com.chinacoal.hr.levelimpl.dao.OrganizationDao;
import com.chinacoal.hr.levelimpl.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-29 22:48
 * @description:
 **/
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationDao organizationDao;

    /**
     * 单纯获取组织机构 isLeaf为n 的数据
     *
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Organization>
     * @author : YunTao.Li
     * @date : 2019/11/29 2019/11/29
     */
    @Override
    public List<Organization> getAbsoluteOrganizations(String parentOrgId) throws Throwable {
        Map<String, Object> condition = new HashMap<String, Object>();
//        condition.put("isLeaf", "n");
        condition.put("orgLevel", "3");
//        condition.put("parentOrgId", parentOrgId);

        List<Organization> organizations = this.organizationDao.getOrganizationList(condition);
        return organizations;
    }

    /**
     * 单纯获取组织机构 isLeaf为y 的数据(部门)
     *
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Organization>
     * @author : YunTao.Li
     * @date : 2019/11/29 2019/11/29
     */
    @Override
    public List<Organization> getAbsoluteDepartments(String parentOrgId) throws Throwable {
        Map<String, Object> condition = new HashMap<String, Object>();
//        condition.put("isLeaf", "y");
        condition.put("orgLevelThen", "4");
        condition.put("parentOrgId", parentOrgId);

        List<Organization> organizations = this.organizationDao.getOrganizationList(condition);
        return organizations;
    }


    @Override
    public Organization getOrganization(String orgId) throws Throwable {
        return this.organizationDao.getOrganization(orgId);
    }

    @Override
    public Organization getOrganizationByCode(String orgCode) throws Throwable {
        Map condition = new HashMap();
        condition.put("orgCode", orgCode);
        return this.organizationDao.getOrganization(condition);
    }

    /**
     * 根据orgid查找org对象，如果发现isleaf不匹配则找parent org，直到找到为止
     *
     * @param orgId  :
     * @param isLeaf :
     * @return : com.chinacoal.hr.levelimpl.entity.Organization
     * @author : YunTao.Li
     * @date : 2020/1/7 2020/1/7
     */
    @Override
    public Organization getOrganizationRecursion(String orgId, String isLeaf) throws Throwable {
        Organization organization = this.organizationDao.getOrganization(orgId);

        if (isLeaf.equals(organization.getIsLeaf())) {
            return organization;
        } else {
            return getOrganizationRecursion(organization.getParentOrgId(), isLeaf);
        }
    }

    /**
     * 查找人员所属的机构，如果发现此机构编码不是纯数字则为部门找上级机构
     *
     * @param orgId :
     * @return : com.chinacoal.hr.levelimpl.entity.Organization
     * @author : YunTao.Li
     * @date : 2020/7/9 2020/7/9
     */
    @Override
    public Organization getOrganizationRecursion(String orgId) throws Throwable {
        Organization organization = this.organizationDao.getOrganization(orgId);

        if (organization.getOrgId().matches("[0-9]+") && ("3").equals(organization.getOrgLevel())) {
            return organization;
        } else {
            return getOrganizationRecursion(organization.getParentOrgId());
        }
    }


}
